﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace QuickExcel
{
    public partial class AlternateRowShading : Form
    {
        public AlternateRowShading()
        {
            InitializeComponent();
        }

        private void btnApplyFormat_Click(object sender, EventArgs e)
        {
            int iCntr;
            Excel.Range rng = Globals.ThisAddIn.Application.ActiveSheet.Range(textBox1.Text);

            for (iCntr = 1; iCntr <= rng.Rows.Count; iCntr= iCntr + 2)
            {
                rng.Rows[iCntr].Interior.Color = firstColorBox.BackColor;
                if (iCntr + 1 <= rng.Rows.Count)
                {
                    rng.Rows[iCntr + 1].Interior.Color = secondColorbox.BackColor;
                }
            }

            if (chkBorder.Checked == true)
            {
                rng.Borders.LineStyle = false;
                rng.Borders.LineStyle = 1;
                rng.Borders.ColorIndex = 0;
                rng.Borders.TintAndShade = 0;
                rng.Borders.Weight = 2;
                rng.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = false;
                rng.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = false;
            }

            if (chkCloseAfterApply.Checked == true)
                this.Dispose();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {             
            colorDialog1.ShowDialog();
            firstColorBox.BackColor = colorDialog1.Color;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            secondColorbox.BackColor = colorDialog1.Color;
        }

        private void AlternateRowShading_Load(object sender, EventArgs e)
        {
            textBox1.Text = Globals.ThisAddIn.Application.Selection.Address;            
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            Excel.Range rng;
            rng = Globals.ThisAddIn.Application.InputBox("Please select range","Select Range",textBox1.Text,this.Left-5,this.Top+5,Type: 8);
            rng.Select();
            textBox1.Text = rng.Address;
            this.Activate();
        }
    }
}
